package com.lxl.securitypermission.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.lxl.securitypermission.constant.HttpCode;
import com.lxl.securitypermission.controller.vo.MenuVo;
import com.lxl.securitypermission.dto.ResponseResult;
import com.lxl.securitypermission.entity.SMenu;
import com.lxl.securitypermission.entity.SRoleMenu;
import com.lxl.securitypermission.mapper.SMenuMapper;
import com.lxl.securitypermission.mapper.SRoleMenuMapper;
import com.lxl.securitypermission.service.MenuService;
import com.lxl.securitypermission.vo.TreeMenu;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 绫小路
 * @date 2020/12/30 21:57
 * @description
 */
@Service
public class MenuServiceImpl implements MenuService {

  @Autowired
  private SMenuMapper sMenuMapper;
  @Autowired
  private SRoleMenuMapper sRoleMenuMapper;

  @Override
  public List<SMenu> getMenuList(Integer userId) {
    return sMenuMapper.get(userId);
  }

  @Override
  public ResponseResult<List<TreeMenu>> getTreeMenu(Integer status) {
    if (status != null && status != 1 && status != 0) {
      status = null;
    }
    List<TreeMenu> result = sMenuMapper.getTreeMenu(status);
    //添加一级根目录
    TreeMenu root = new TreeMenu();
    root.setId(0);
    root.setPid(-1);
    root.setTitle("菜单根目录");
    result.add(root);
    return new ResponseResult<List<TreeMenu>>(HttpCode.OK, result);
  }

  @Override
  public ResponseResult<SMenu> info(int id) {
    return new ResponseResult<>(HttpCode.OK, sMenuMapper.selectByPrimaryKey(id));
  }

  @Override
  public ResponseResult update(MenuVo menuVo) {
    SMenu menu = BeanUtil.copyProperties(menuVo, SMenu.class);
    menu.setUpdateTime(new Date());
    sMenuMapper.updateByPrimaryKeySelective(menu);
    return new ResponseResult(HttpCode.OK, "更新成功！");
  }

  @Override
  public ResponseResult add(MenuVo menuVo) {
    SMenu menu = BeanUtil.copyProperties(menuVo, SMenu.class);
    menu.setCreateTime(new Date());
    sMenuMapper.insertSelective(menu);
    return new ResponseResult(HttpCode.OK, "添加成功！");
  }


  @Transactional//开启事务
  @Override
  public ResponseResult delete(Integer id) {
    List<Integer> ids = findSonId(id);//递归获取
    int res = sMenuMapper.del(ids);
    return new ResponseResult(HttpCode.OK, "删除成功！删除节点数：" + res);
  }

  /**
   * 递归查找id
   */
  private List<Integer> findSonId(Integer id) {
    List<Integer> res = new ArrayList<>();
    res.add(id);
    List<Integer> sonids = sMenuMapper.getSonMenuId(id);
    if (sonids.isEmpty()) {
      return res;
    }
    for (Integer i : sonids) {
      res.addAll(findSonId(i));//递归
    }
    return res;
  }

  @Override
  public ResponseResult permissionInfo(Integer menuId) {
    List<SRoleMenu> res = sRoleMenuMapper.get(menuId);
    return new ResponseResult(HttpCode.OK, res);
  }

  @Override
  public ResponseResult updatePermission(Integer menuId, String roleIds) {
    if (roleIds.isEmpty()) {
      if (sRoleMenuMapper.hasByMenuId(menuId) > 0) {
        sRoleMenuMapper.deleteByMenuId(menuId);
      }
    } else {
      String[] rids = roleIds.split(",");
      for (int j = 0; j < rids.length; j++) {
        int rid = Integer.valueOf(rids[j].trim());//注意去空格！
        if (sRoleMenuMapper.has(rid, menuId) > 0) {
          //已经存在，跳出
          continue;
        }
        SRoleMenu roleMenu = new SRoleMenu();
        roleMenu.setMenuId(menuId);
        roleMenu.setRoleId(rid);
        sRoleMenuMapper.insertSelective(roleMenu);
      }
    }
    return new ResponseResult(HttpCode.OK, "修改成功！");
  }

  /**
   * 递归获取父级id
   *
   * @return
   */
  private List<Integer> findParentId(Integer menuId) {
    List<Integer> res = new ArrayList<>();
    res.add(menuId);
    SMenu menu = sMenuMapper.selectByPrimaryKey(menuId);
    if (menu.getPid() == 0) {
      return res;
    } else {
      res.add(menu.getPid());
      res.addAll(findParentId(menu.getPid()));
    }
    return res;
  }
}
